#!/bin/bash
PF=${PN}-${PVR}

die() {
	echo "******"
	echo $1
	echo "******"
	exit 1
}

if [ $1 = "install" ]; then
	declare INSTALL_DIR="${VHOST_ROOT}/${PF}"
	# edit Apache configs
	cd ${VHOST_ROOT}/${PF}/scripts
	sed -e "s|/opt/otrs/var/httpd/htdocs/|${MY_INSTALLDIR}/|g" -i apache-httpd.include.conf apache2-httpd.include.conf || die "sed failed"
	
	# set $HOME
	grep -lR "/opt" * | xargs sed -i "s|/opt/otrs|${VHOST_ROOT}/${PF}|g" || die "sed failed"
	
	sed -i "s|/opt/otrs|${VHOST_ROOT}/${PF}|g" ${VHOST_ROOT}/${PF}/Kernel/Config.pm || die "sed failed"

	# set permissions
	chmod +x "${INSTALL_DIR}"/bin/SetPermissions.pl
	/usr/bin/perl "${INSTALL_DIR}/bin/"SetPermissions.pl   --otrs-user=otrs --web-user=apache --otrs-group=apache --web-group=apache --admin-user=root  \
					${VHOST_ROOT}/${PF} > /dev/null || die "Could not set permissions"


elif [ $1 = "clean" ]; then
	echo "Please examine the contents of the following directories"
	echo "and delete anything that is no longer necessary"
	echo
	echo ${VHOST_ROOT}/${PF}
	echo ${MY_INSTALLDIR}
fi
